//
//  BKMyOrderTitleView.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderTitleView.h"
#import "BK_OD_Color.h"
#import "BK_OD_Tool.h"

#define BTNHEIGHT    33

@interface BKMyOrderTitleView()<UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView * scrView;
@property (nonatomic,strong)NSMutableArray * btnArr; //titleBtn数组
@property (nonatomic,strong)NSArray * nameArr;
@property (nonatomic,strong)UIView *  bottomLineView; //底部指示view
@property (nonatomic,assign)CGFloat   scrContentOffsetWidth;

@end

@implementation BKMyOrderTitleView
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setUpViewsWithCategoryNames:[NSArray arrayWithObjects:@"全部",@"待付款",@"待发货",@"待收货",@"待评价",nil]];
    }
    return self;
}

-(void)setUpViewsWithCategoryNames:(NSArray *)names{
    
    self.nameArr = names;
    UIScrollView * scrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, self.frame.size.height)];
    self.scrView = scrView;
    scrView.contentSize = CGSizeMake(ScreenWidth, 36);
    scrView.showsHorizontalScrollIndicator = NO;
    scrView.showsVerticalScrollIndicator = NO;
    scrView.delegate = self;
    scrView.scrollEnabled = YES;
    [self addSubview:scrView];
    
    _btnArr = [NSMutableArray new];
    for (int i = 0; i < names.count; i ++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i;
        button.titleLabel.font = Font14;
        
        [button setTitle:names[i] forState:UIControlStateNormal];
        [button setTitleColor:kOrderLightGrey1 forState:UIControlStateNormal];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitleColor:kOrderBKGreen forState:UIControlStateSelected];
        [button addTarget:self action:@selector(titleBtnTouch:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrView addSubview:button];
        [_btnArr addObject:button];
    }
    UIView * bottomLineView = [[UIView alloc]init];
    self.bottomLineView = bottomLineView;
    bottomLineView.backgroundColor = kOrderBKGreen;
    [self.scrView addSubview:bottomLineView];
        [self layoutSubview];
}

-(void)layoutSubview{
    
    _scrView.scrollEnabled = NO;
    CGFloat buttonWidth = 0;
    buttonWidth = self.frame.size.width/self.nameArr.count;
    for (int i = 0; i < self.btnArr.count; i++) {
        
        UIButton * btn = self.btnArr[i];
        btn.frame = CGRectMake(i*buttonWidth, 0, buttonWidth, BTNHEIGHT);
        if (i == 0) {
            btn.selected = YES;
            CGSize size = [self getSizeWithText:btn.titleLabel.text];
            [_bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.centerX.mas_equalTo(btn.mas_centerX);
                make.top.mas_equalTo(btn.mas_bottom).offset(0);
                make.width.mas_equalTo(size.width);
                make.height.mas_equalTo(@(3));
            }];
        }
    }
}

-(void)setCurPage:(NSInteger)curPage{
    _curPage = curPage;
    UIButton *button = self.btnArr[_curPage];
    [self titleBtnTouch:button];
}

//点击button响应事件
-(void)titleBtnTouch:(UIButton *)button{

    NSInteger tagValue = button.tag;
    for (UIButton * btn in self.btnArr) {
        if (btn.tag != tagValue) {
            btn.selected = NO;
        }else{
            btn.selected = YES;
        }
    }
    CGSize size = [self getSizeWithText:button.titleLabel.text];
    //设置指示条位置
    [UIView animateWithDuration:0.5 animations:^{
        [self.bottomLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(button.mas_centerX);
            make.top.mas_equalTo(button.mas_bottom).offset(0);
            make.width.mas_equalTo(size.width);
            make.height.mas_equalTo(@(3));
        }];
        
    }];
    
    if ([self.delegate respondsToSelector:@selector(BK_Order_Delegate_TitleButton_Click:)]) {
        [self.delegate BK_Order_Delegate_TitleButton_Click:button];
    }
}

/**
 *   获取字符宽度
 */
- (CGSize)getSizeWithText:(NSString*)text{
    NSMutableParagraphStyle* style = [[NSMutableParagraphStyle alloc] init];
    style.lineBreakMode = NSLineBreakByCharWrapping;
    CGSize size = [text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 24) options: NSStringDrawingUsesLineFragmentOrigin   attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14.0f],NSParagraphStyleAttributeName:style} context:nil].size;
    return CGSizeMake(size.width, 24);
}

@end















































